import axios from "axios"

// 创建axios的实例
const instance = axios.create({
	baseURL: "https://api.chatanywhere.tech", // 假设这是你的API的基础URL
	headers: {
		"Content-Type": "application/json",
		Authorization: "Bearer sk-bJRqYDa5GWT2QyluR8DKvd1EJenAqAMNbC75xIIZfGFgFtVc",
		"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
	},
})

let loading = null

// 请求拦截器
instance.interceptors.request.use(
	(config) => {
		return config
	},
	(error) => {
		return Promise.reject(error)
	}
)

// 响应拦截器
instance.interceptors.response.use(
	(response) => {
		return response.data
	},
	(error) => {
		return Promise.reject(error)
	}
)

/**
 * 发送消息到API并获取响应
 * @param {Object} paramConfig - 包含请求参数的配置对象
 * @returns {Promise} - 包含API响应的Promise对象
 */
const request = (paramConfig) => {
	const { url, message, role } = paramConfig

	// 准备发送的数据
	const data = JSON.stringify({
		model: "gpt-3.5-turbo",
		n: 1,
		messages: [
			{
				role: "system",
				content: role,
			},
			{
				role: "user",
				content: message,
			},
		],
	})

	// 发起 POST 请求，捕获可能的错误
	return instance.post(url, data)
}

export default request
